<template>
	<el-card>
		<el-form :inline="true" :model="queryData" class="demo-form-inline">
			<el-form-item label="标题">
				<el-input v-model="queryData.query" placeholder="请输入标题" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="onSubmit(1)">搜索</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="pageData.list" border style="width: 100%">
			<el-table-column prop="msg_title" label="标题" />
			<el-table-column prop="msg_cont" label="内容" />
			<el-table-column prop="start_time" label="开始时间" />
			<el-table-column prop="end_time" label="结束时间" />
			<el-table-column prop="msg_recipient" label="接受人">
				<template #default="scpot">
					{{ scpot.row.msg_recipient == '0' ? '所有人' : scpot.row.msg_recipient }}
				</template>
			</el-table-column>
			<el-table-column prop="address" label="操作" width="100">
				<template #default="scpot">
					<el-button link type="danger" @click="deletMessages(scpot.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination
			:current="queryData.pageNum"
			:total="pageData.total"
			:pageSize="queryData.pageSize"
			@paginaClick="onSubmit"
		></pagination>
	</el-card>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { getMessage, deletMsg } from '@/utils/api';
import { ElMessage, ElMessageBox } from 'element-plus';
const pageData: any = ref({});
const queryData = reactive({
	query: '',
	pageNum: 1,
	pageSize: 10,
});
const getMessageList = () => {
	getMessage(queryData).then((res: any) => {
		pageData.value = res.data;
	});
};
getMessageList();
const onSubmit = (val: number) => {
	queryData.pageNum = val;
	getMessageList();
};
const deletMessages = (row: any) => {
	ElMessageBox.confirm('确定要删除该条消息吗?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		deletMsg({
			id: row.id,
		}).then((res: any) => {
			ElMessage.success('删除成功');
			if (queryData.pageNum > 1 && pageData.value.list.length == 1) {
				queryData.pageNum -= 1;
			}
			getMessageList();
		});
	});
};
</script>
